package com.eian.boot.common.core.exception;

import com.eian.boot.common.core.exception.core.AbstractBaseEnum;
import org.apache.commons.lang3.ObjectUtils;

/**
 * 业务异常工具类
 *
 * @author alex.meng
 */
public final class ExceptionHelper {

    private ExceptionHelper() {
    }

    /**
     * 创建业务异常
     *
     * @param message 错误信息
     */
    public static void error(String message) {
        throw new BusinessException(message);
    }

    /**
     * 创建业务异常
     *
     * @param code    错误码
     * @param message 错误信息
     */
    public static void error(Integer code, String message) {
        throw new BusinessException(code, message);
    }

    /**
     * 抛出异常
     *
     * @param err 错误信息
     */
    public static void error(AbstractBaseEnum err) {
        throw new BusinessException(err.getCode(), err.getDesc());
    }

    // ========================= 断言快速处理 ===========================

    public static void ifNull(Object obj, String message) {
        if (ObjectUtils.isEmpty(obj)) {
            throw new BusinessException(message);
        }
    }

    public static void ifNull(Object obj, AbstractBaseEnum err) {
        if (ObjectUtils.isEmpty(obj)) {
            throw new BusinessException(err.getCode(), err.getDesc());
        }
    }

    public static void ifNull(Object obj, Integer code, String message) {
        if (ObjectUtils.isEmpty(obj)) {
            throw new BusinessException(code, message);
        }
    }

    public static void ifNotNull(Object obj, String message) {
        if (ObjectUtils.isNotEmpty(obj)) {
            throw new BusinessException(message);
        }
    }

    public static void ifNotNull(Object obj, AbstractBaseEnum err) {
        if (ObjectUtils.isNotEmpty(obj)) {
            throw new BusinessException(err.getCode(), err.getDesc());
        }
    }

    public static void ifNotNull(Object obj, Integer code, String message) {
        if (ObjectUtils.isNotEmpty(obj)) {
            throw new BusinessException(code, message);
        }
    }

    public static void ifTrue(boolean flag, String message) {
        if (flag) {
            throw new BusinessException(message);
        }
    }

    public static void ifTrue(boolean flag, AbstractBaseEnum err) {
        if (flag) {
            throw new BusinessException(err.getCode(), err.getDesc());
        }
    }

    public static void ifTrue(boolean flag, Integer code, String message) {
        if (flag) {
            throw new BusinessException(code, message);
        }
    }

    public static void ifFalse(boolean flag, String message) {
        if (!flag)
            throw new BusinessException(message);
    }

    public static void ifFalse(boolean flag, AbstractBaseEnum err) {
        if (!flag)
            throw new BusinessException(err.getCode(), err.getDesc());
    }

    public static void ifFalse(boolean flag, Integer code, String message) {
        if (!flag)
            throw new BusinessException(code, message);
    }

    public static void ifEqual(Object o1, Object o2, String message) {
        if (o1.equals(o2))
            throw new BusinessException(message);
    }

    public static void ifEqual(Object o1, Object o2, AbstractBaseEnum err) {
        if (o1.equals(o2))
            throw new BusinessException(err.getCode(), err.getDesc());
    }

    public static void ifEqual(Object o1, Object o2, Integer code, String message) {
        if (o1.equals(o2))
            throw new BusinessException(code, message);
    }

    public static void ifNotEqual(Object o1, Object o2, String message) {
        if (!o1.equals(o2))
            throw new BusinessException(message);
    }

    public static void ifNotEqual(Object o1, Object o2, AbstractBaseEnum err) {
        if (!o1.equals(o2))
            throw new BusinessException(err.getCode(), err.getDesc());
    }

    public static void ifNotEqual(Object o1, Object o2, Integer code, String message) {
        if (!o1.equals(o2))
            throw new BusinessException(code, message);
    }
}